package com.dianrui.agent.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.PolygonOptions;
import com.dianrui.agent.R;
import com.dianrui.agent.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OperationAreaActivity extends BaseActivity {
    private AMap aMap;
    private List<LatLng> marea_points = new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.mapviews)
    MapView mapviews;


    @Override
    public int getContentViewId() {
        return R.layout.activity_operation_area;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapviews.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapviews.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapviews.onPause();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapviews.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapviews.onDestroy();
    }

    @Override
    public void initView() {
        title.setText("运营区域");
        if (aMap == null) {
            aMap = mapviews.getMap();
        }
        UiSettings mUiSettings = aMap.getUiSettings();
        mUiSettings.setTiltGesturesEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);
        mUiSettings.setZoomControlsEnabled(false);
        getArea();
    }

    private void getArea() {
        if (!StringUtils.isEmpty(spUtils.getString("area"))) {
            try {
                final JSONObject jsonObject = new JSONObject(spUtils.getString("area"));
                if (jsonObject != null) {
                    if (marea_points != null) {
                        marea_points.clear();
                    }
                    JSONArray gfixed_path = jsonObject.optJSONObject("data").optJSONArray("fixed_path");
                    if (gfixed_path.length() > 0) {
                        for (int j = 0; j < gfixed_path.length(); j++) {
                            JSONObject jsonObject3 = gfixed_path.optJSONObject(j);
                            marea_points.add(new LatLng(jsonObject3.optDouble("lat"), jsonObject3.optDouble("lng")));

                        }

                        if (marea_points.size() > 0) {
                            PolygonOptions polygonOptions = new PolygonOptions();
                            polygonOptions.addAll(marea_points);
                            polygonOptions.strokeWidth(6);
                            polygonOptions.strokeColor(Color.rgb(255, 0, 0));
                            polygonOptions.fillColor(Color.argb(15, 255, 0, 0));
                            aMap.addPolygon(polygonOptions);
                        }
                        LatLngBounds.Builder b = LatLngBounds.builder();

                        for (LatLng latLng : marea_points) {
                            b.include(latLng);
                        }
                        LatLngBounds bounds = b.build();
                        aMap.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(bounds, 80, 80, 230, 260));
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }



    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
